//
//  FoundTableViewCell.m
//  item-NetEaseNews-tyw
//
//  Created by tyw on 15/10/5.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "FoundTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "FoundModel.h"

@implementation FoundTableViewCell {
    UIImageView* bigImgView;            //大图
    
    UIImageView* smallIconImgView;      //小图标
    UILabel* nameLabel;                 //名字，考拉海购、秀品···
    
    UILabel* titleLabel;                //标题
    UILabel* subTitleLabel;             //子标题
    
    UILabel* redBtnContenLabel;         //现价
    UILabel* blackBtnContentLabel;      //原价
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.userInteractionEnabled = YES;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self _createSubViews];
    }
    
    return self;
}

- (void)_createSubViews {
    //大图
    bigImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 40, kWidth, 200)];
    [self.contentView addSubview:bigImgView];
    
    //小图标
    smallIconImgView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 40, 40)];
    smallIconImgView.layer.cornerRadius = 5;
    smallIconImgView.layer.masksToBounds = YES;
    [self.contentView addSubview:smallIconImgView];
    
    //名字
    nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(smallIconImgView.right+10, 20, 100, 15)];
    nameLabel.textColor = [UIColor blackColor];
    nameLabel.font = [UIFont systemFontOfSize:10];
    [self.contentView addSubview:nameLabel];
    
    
    //标题
    titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, bigImgView.bottom+10, kWidth-40, 20)];
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:titleLabel];
    //子标题
    subTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, titleLabel.bottom+5, kWidth-40, 20)];
    subTitleLabel.textColor = [UIColor grayColor];
    subTitleLabel.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:subTitleLabel];
    
    //红色的，有时候是现价
    redBtnContenLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, subTitleLabel.bottom+5, 100, 30)];
    redBtnContenLabel.textColor = [UIColor redColor];
    redBtnContenLabel.font = [UIFont systemFontOfSize:14];
    redBtnContenLabel.textAlignment = NSTextAlignmentCenter;
    redBtnContenLabel.layer.cornerRadius = 5;
    redBtnContenLabel.layer.borderWidth = 1;
    redBtnContenLabel.layer.borderColor = [UIColor redColor].CGColor;
    redBtnContenLabel.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:redBtnContenLabel];
    //黑色的，有时候是原价
    blackBtnContentLabel = [[UILabel alloc] initWithFrame:CGRectMake(redBtnContenLabel.right+20, subTitleLabel.bottom+5, kWidth-150, 30)];
    blackBtnContentLabel.textColor = [UIColor grayColor];
    blackBtnContentLabel.font = [UIFont systemFontOfSize:10];
    [self.contentView addSubview:blackBtnContentLabel];
}

- (void)setDataDic:(NSDictionary *)dataDic {
    if (_dataDic != dataDic) {
        _dataDic = [dataDic copy];
        [self setNeedsLayout];
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    //获得模型
    NSArray* contextArray = _dataDic[@"context"];
    if (!contextArray.count) return;
    FoundModel* model = [[FoundModel alloc] initWithDataDic:contextArray[0]];
    
    //大图
    NSString* bigImgUrl = model.bigImgUrl;
    [bigImgView sd_setImageWithURL:[NSURL URLWithString:bigImgUrl]];
    
    //小图标
    [smallIconImgView sd_setImageWithURL:[NSURL URLWithString:_dataDic[@"icon"]]];
    
    //名字
    nameLabel.text = _dataDic[@"name"];
    
    //title
    titleLabel.text = model.title;
    
    //subTitle
    subTitleLabel.text = model.subTitle;
    
    //红色的
    redBtnContenLabel.text = model.redBtnContent;
    
    //黑色的
    blackBtnContentLabel.text = model.blackBtnContent;
}
@end
